package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.constant.Constants;
import com.ruibang.glass.quality.domain.req.DefectSizeDataReq;
import com.ruibang.glass.quality.domain.req.DiscardDefectDataReq;
import com.ruibang.glass.quality.domain.req.DiscardDefectReq;
import com.ruibang.glass.quality.domain.req.LayeredPointDataReq;
import com.ruibang.glass.quality.domain.resp.DefectMapDataResp;
import com.ruibang.glass.quality.domain.resp.DefectSizeDataResp;
import com.ruibang.glass.quality.domain.resp.LayeredPointResp;
import com.ruibang.glass.quality.entity.BandDefectData;
import com.ruibang.glass.quality.entity.DictEntity;
import com.ruibang.glass.quality.mapper.BandDefectDataMapper;
import com.ruibang.glass.quality.service.BandDefectDataService;
import com.ruibang.glass.quality.service.DiscardDefectDataService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2023-09-18  21:19
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class BandDefectDataServiceImpl extends ServiceImpl<BandDefectDataMapper, BandDefectData> implements BandDefectDataService {

    @Autowired
    private DiscardDefectDataService discardDefectDataService;

    @Override
    public void deleteByDate(DiscardDefectReq defectReq) {
        baseMapper.deleteByDate(defectReq);
    }


    /**
     * @Description: 溶解缺陷map图
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-31 9:44
     */
    @Override
    public ResultBody queryDefectMapData(DiscardDefectDataReq discardDefectDataReq) {
        String defectName = discardDefectDataReq.getDefectName();
        String dictGroupType = discardDefectDataReq.getDictGroupType();
        LocalDateTime startTime = discardDefectDataReq.getStartTime();
        LocalDateTime endTime = discardDefectDataReq.getEndTime();
        //判断是否是全部类型
        if (Constants.DEFECT_DICT_GROUP_TYPE.equals(dictGroupType)) {
            List<DictEntity> dictList = null;
            if (defectName.contains(Constants.DEFECT_BUBBLE)) {
                dictList = discardDefectDataService.getBubbleAll();
            } else if (defectName.contains(Constants.DEFECT_PT)) {
                dictList = discardDefectDataService.getPTAll();
            } else {
                log.error("没有查询到缺陷数据");
            }
            //缺陷类型不可为null
            if (CollectionUtils.isNotEmpty(dictList)) {
                List<String> defect = dictList.stream().map(DictEntity::getValue).collect(Collectors.toList());
                List<BandDefectData> bandDefectDataList = baseMapper.queryAllDefectMapData(startTime, endTime, defect);
                return ResultBody.success(setDefectMpaData(bandDefectDataList));
            }
        } else {
            //单个类型
            List<BandDefectData> bandDefectDataList = baseMapper.queryDefectMapData(discardDefectDataReq);
            return ResultBody.success(setDefectMpaData(bandDefectDataList));
        }
        return ResultBody.success(Collections.emptyList());
    }


    /**
     * @Description: 判废的气泡、结石、铂金的尺寸图
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-31 11:01
     */
    @Override
    public ResultBody queryDefectSizeData(DefectSizeDataReq defectSizeDataReq) {
        String defectName = defectSizeDataReq.getDefectName();
        String dictGroupType = defectSizeDataReq.getDictGroupType();
        LocalDateTime startTime = defectSizeDataReq.getStartTime();
        LocalDateTime endTime = defectSizeDataReq.getEndTime();
        //判断是否是全部类型
        if (Constants.DEFECT_DICT_GROUP_TYPE.equals(dictGroupType)) {
            List<DictEntity> dictList = null;
            if (defectName.contains(Constants.DEFECT_BUBBLE)) {
                dictList = discardDefectDataService.getBubbleAll();
            } else if (defectName.contains(Constants.DEFECT_PT)) {
                dictList = discardDefectDataService.getPTAll();
            } else {
                log.error("没有查询到缺陷数据");
            }
            //缺陷类型不可为null
            if (CollectionUtils.isNotEmpty(dictList)) {
                List<String> defect = dictList.stream().map(DictEntity::getValue).collect(Collectors.toList());
                List<BandDefectData> bandDefectDataList = baseMapper.queryAllDefectSizeData(startTime, endTime, defect);
                return getDefectSizeData(bandDefectDataList);
            }
        } else {
            //单个类型
            List<BandDefectData> bandDefectDataList = baseMapper.queryDefectSizeData(defectSizeDataReq);
            return getDefectSizeData(bandDefectDataList);
        }
        return ResultBody.success(Collections.emptyList());
    }


    /**
     * @Description: 分层点状图
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-31 10:49
     */
    @Override
    public ResultBody queryLayeredPointData(LayeredPointDataReq layeredPointDataReq) {
        String defectName = layeredPointDataReq.getDefectName();
        String dictGroupType = layeredPointDataReq.getDictGroupType();
        LocalDateTime startTime = layeredPointDataReq.getStartTime();
        LocalDateTime endTime = layeredPointDataReq.getEndTime();
        //判断是否是全部类型
        List<String> defectData = new ArrayList<>();
        if (Constants.DEFECT_DICT_GROUP_TYPE.equals(dictGroupType)) {
            List<DictEntity> dictList = null;
            if (defectName.contains(Constants.DEFECT_BUBBLE)) {
                dictList = discardDefectDataService.getBubbleAll();
            } else if (defectName.contains(Constants.DEFECT_PT)) {
                dictList = discardDefectDataService.getPTAll();
            } else {
                log.error("没有查询到缺陷数据");
            }
            //缺陷类型不可为null
            if (CollectionUtils.isNotEmpty(dictList)) {
                defectData = dictList.stream().map(DictEntity::getValue).collect(Collectors.toList());
            }
        } else {
            //单个类型
            defectData = Collections.singletonList(layeredPointDataReq.getDefectName());
        }

        List<BandDefectData> bandDefectDataList = baseMapper.queryLayeredPointDataReq(startTime, endTime, defectData);

        if (CollectionUtils.isNotEmpty(bandDefectDataList)) {
            List<LayeredPointResp> layeredPointRespList = new ArrayList<>();
            for (BandDefectData bandDefectData : bandDefectDataList) {
                LayeredPointResp layeredPointResp = new LayeredPointResp();
                layeredPointResp.setDefectName(bandDefectData.getDefectName());
                layeredPointResp.setPointX(bandDefectData.getStationStartTime());
                layeredPointResp.setPointY(bandDefectData.getTiltedDiv());
                layeredPointRespList.add(layeredPointResp);
            }
            layeredPointRespList = layeredPointRespList.stream().sorted(Comparator.comparing(LayeredPointResp::getPointX)).collect(Collectors.toList());
            return ResultBody.success(layeredPointRespList);
        }
        return ResultBody.success(Collections.emptyList());
    }


    /**
     * @Description: 设置判废的气泡、结石、铂金的尺寸图数据
     * @version v1.0
     * @author songJinKang
     * @date 2023-09-06 19:38
     */
    private ResultBody getDefectSizeData(List<BandDefectData> discardDefectData) {
        List<DefectSizeDataResp> defectSizeDataRespList = new ArrayList<>();
        for (BandDefectData bandDefectData : discardDefectData) {
            DefectSizeDataResp defectSizeDataResp = new DefectSizeDataResp();
            defectSizeDataResp.setDefectName(bandDefectData.getDefectName());
            defectSizeDataResp.setPointX(bandDefectData.getStationStartTime());
            defectSizeDataResp.setPointY(bandDefectData.getTiltedSize());
            defectSizeDataRespList.add(defectSizeDataResp);
        }
        defectSizeDataRespList = defectSizeDataRespList.stream().sorted(Comparator.comparing(DefectSizeDataResp::getPointX)).collect(Collectors.toList());
        return ResultBody.success(defectSizeDataRespList);
    }


    /**
     * @Description: 设置溶解缺陷map图数据
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-31 10:42
     */
    private List<DefectMapDataResp> setDefectMpaData(List<BandDefectData> discardDefectData) {
        if (CollectionUtils.isNotEmpty(discardDefectData)) {
            List<DefectMapDataResp> defectMapDataRespList = new ArrayList<>();
            for (BandDefectData bandDefectData : discardDefectData) {
                DefectMapDataResp defectMapDataResp = new DefectMapDataResp();
                defectMapDataResp.setDefectName(bandDefectData.getDefectName());
                defectMapDataResp.setPointX(bandDefectData.getStationStartTime());
                defectMapDataResp.setPointY(bandDefectData.getMaterialCenterCW());
                defectMapDataRespList.add(defectMapDataResp);
            }
            defectMapDataRespList = defectMapDataRespList.stream().sorted(Comparator.comparing(DefectMapDataResp::getPointX)).collect(Collectors.toList());
            return defectMapDataRespList;
        }
        return Collections.emptyList();
    }


}
